package ppt;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hslf.usermodel.*;

public class pptToImage {

	public static int toImg(String path, String name) throws IOException {
		// 创建输入流
		FileInputStream is = new FileInputStream(name);
		// 构造一个幻灯片文档输入流
		HSLFSlideShow ppt = new HSLFSlideShow(is);
		// 关闭输入流
		is.close();
		// 获取幻灯片高度与宽度
		Dimension pgsize = ppt.getPageSize();
		// 放大倍数
		double multiple;

		// 用于标识当前保存图片的页数
		int idx = 1;

		for (HSLFSlide slide : ppt.getSlides()) {
			//设置字体
			for (java.util.List<HSLFTextParagraph> txt : slide.getTextParagraphs()) {
				for (HSLFTextParagraph para : txt) {
					for (HSLFTextRun run : para) {
						run.setFontFamily("宋体");
					}
				}
			}
			// 设置图片属性
			BufferedImage img = new BufferedImage((int) (pgsize.width), (int) (pgsize.height),
					BufferedImage.TYPE_INT_RGB);
			// 绘图
			Graphics2D graphics = img.createGraphics();
			graphics.setPaint(Color.white);
			//设置绘图倍数
			graphics.scale(1,1);
			// 渲染
			slide.draw(graphics);
			// 设置目标文件
			File toFile = new File(path, idx + ".png");
			// 保存和输出
			FileOutputStream out = new FileOutputStream(toFile);
			// 设置输出格式
			javax.imageio.ImageIO.write(img, "png", out);
			// 关闭输出流
			out.close();
			idx++;
			System.out.println("转换页数" + (idx-1));
		}
		//返回页数
		return idx - 1;
	}
}
